package com.app.zenmewan.controller;

import com.app.zenmewan.commons.Actions;
import com.app.zenmewan.commons.Message;
import com.app.zenmewan.entity.Comment;
import com.app.zenmewan.entity.ScenComment;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.ScenCommentService;
import com.app.zenmewan.service.UserService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (ScenComment)表控制层
 *
 * @author makejava
 * @since 2020-05-06 10:13:13
 */
@RestController
@RequestMapping("scenComment")
public class ScenCommentController {
    /**
     * 服务对象
     */
    @Resource
    private ScenCommentService scenCommentService;
    @Resource
    private UserService userService;
    @Autowired
    private Actions actions;
    @Getter
    @Setter
    class Vo{
        ScenComment scenComment;
        String avaterUrl;
        String nickname;
        int level;
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ScenComment selectOne(Integer id) {
        return this.scenCommentService.queryById(id);
    }
    @GetMapping("list/{sid}")
    public ModelAndView list(@PathVariable("sid") int sid) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        ScenComment comment = new ScenComment();
        comment.setSid(sid);
        List<ScenComment> list = this.scenCommentService.queryAll(comment);
        List<Vo> l = new ArrayList<>();
        if(list.size()>0){
            for (ScenComment scenComment : list) {
                int uid = scenComment.getUid();
                User user = this.userService.queryById(uid);
                int level = (int) Math.floor(Math.log(user.getPoint()) * 1.02);
                Vo vo = new Vo();
                vo.avaterUrl = user.getAvataurl();
                vo.level = level;
                vo.nickname = user.getNickname();
                vo.scenComment = scenComment;
                l.add(vo);
            }
            modelAndView.addObject("list",l);
        }
        return modelAndView;
    }

    @PostMapping("submit/{token}")
    public ModelAndView submit(@RequestBody ScenComment scenComment,@PathVariable("token")String token){
        int user_id = this.actions.checkUid(token);
        ScenComment s = new ScenComment();
        s.setUid(user_id);
        s.setSid(scenComment.getSid());
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        if(this.scenCommentService.queryAll(s).size()!=0){
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS,"不能再次对该景点点评哦！");
        }else{
            scenComment.setUid(user_id);
            this.userService.Jiajifen(user_id,15);
            this.scenCommentService.insert(scenComment);
            modelAndView.addObject(Message.TYPE,Message.OK);
        }
        return modelAndView;
    }
    @GetMapping("delete")
    public void delete(int id){
        this.scenCommentService.deleteById(id);
    }
}